package com.example.springboottools.sap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedInputStream;
import java.util.*;

public class PropertyUtil {
	private final static Log log = LogFactory.getLog(PropertyUtil.class);
	Properties config = new Properties();
	public PropertyUtil(String fileName) {
		try {
			config.load(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(fileName)));
		} catch (Exception e) {
			log.info("",e);
		}
	}

	/**
	 * @函数介绍：根据关键字取默认配置文件中的属性值
	 * @参数：关键字
	 * @返回值：对应的字符串值
	 */
	public String getPropertyStr(String key) {
		String value = "";
		try {
			// 如果配置文件中有该key
			if (config.containsKey(key))
				value = config.getProperty(key).trim();
			// 如果配置文件中没有该key则value为空
			else {
				System.out.println("配置文件中" + key + "不存在,请核实！");
				// logger.info("配置文件中" + key + "不存在,请核实！");
				value = "";
			}
		} catch (Exception e) {
			log.info("",e);
		}
		return value;
	}

	/**
	 * @函数介绍：根据关键字取默认配置文件中的属性值
	 * @参数：关键字
	 * @返回值：对应的int值
	 */
	public int getPropertyInt(String key) {
		int value = 0;
		try {
			if (config.containsKey(key))
				value = Integer.parseInt(config.getProperty(key).trim());
			// 如果配置文件中没有该key则value为空
			else {
				// logger.info("配置文件中" + key + "不存在,请核实！");
				System.out.println("配置文件中" + key + "不存在,请核实！");
				value = 0;
			}
		} catch (Exception e) {
			log.info("",e);
			return 0;
		}
		return value;
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List gekeyList() {
		List reslist = new ArrayList();
		try {
			Set keyset = config.keySet();
			for (Iterator it = keyset.iterator(); it.hasNext();) {
				Object lkey = it.next();
				reslist.add(lkey);
			}

		} catch (Exception e) {
			log.info("",e);
		}
		return reslist;

	}

}
